package com.zlzlib.liblayoutmanager;

import android.content.Context;
import android.view.View;

/**
 * @Desc: 圆形滑动缩放的类型
 * @Copyright:
 * @DateTime: 2021/8/17 17:08
 * @Author zlz
 * @Version 1.0
 */
public class LayoutCircleScaleManager extends LayoutCircleManager {

    private float centerScale;

    public LayoutCircleScaleManager(Context context) {
        this(context, 1.2f);
    }

    public LayoutCircleScaleManager(Context context, float centerScale) {
        this(context, INVALID_SIZE, centerScale);
    }

    public LayoutCircleScaleManager(Context context, int radius, float centerScale) {
        super(context, radius);
        this.centerScale = centerScale;
    }

    public LayoutCircleScaleManager(Context context, int radius, int gravity, float centerScale) {
        super(context, radius, gravity);
        this.centerScale = centerScale;
    }

    @Override
    protected void setItemViewProperty(View itemView, float targetOffset) {
        float scale = 1f;
        switch (gravity) {
            case RIGHT:
            case TOP:
                if (flipRotate) {
                    itemView.setRotation(targetOffset);
                    if (targetOffset < angleInterval && targetOffset > -angleInterval) {
                        float diff = Math.abs(Math.abs(itemView.getRotation() - angleInterval) - angleInterval);
                        scale = (centerScale - 1f) / -angleInterval * diff + centerScale;
                    }
                } else {
                    itemView.setRotation(360 - targetOffset);
                    if (targetOffset < angleInterval && targetOffset > -angleInterval) {
                        float diff = Math.abs(Math.abs(360 - itemView.getRotation() - angleInterval) - angleInterval);
                        scale = (centerScale - 1f) / -angleInterval * diff + centerScale;
                    }
                }
                break;
            case LEFT:
            case BOTTOM:
            default:
                if (flipRotate) {
                    itemView.setRotation(360 - targetOffset);
                    if (targetOffset < angleInterval && targetOffset > -angleInterval) {
                        float diff = Math.abs(Math.abs(360 - itemView.getRotation() - angleInterval) - angleInterval);
                        scale = (centerScale - 1f) / -angleInterval * diff + centerScale;
                    }
                } else {
                    itemView.setRotation(targetOffset);
                    if (targetOffset < angleInterval && targetOffset > -angleInterval) {
                        float diff = Math.abs(Math.abs(itemView.getRotation() - angleInterval) - angleInterval);
                        scale = (centerScale - 1f) / -angleInterval * diff + centerScale;
                    }
                }
                break;
        }
        itemView.setScaleX(scale);
        itemView.setScaleY(scale);
    }

    public float getCenterScale() {
        return centerScale;
    }

    public void setCenterScale(float centerScale) {
        assertNotInLayoutOrScroll(null);
        if (this.centerScale == centerScale) return;
        this.centerScale = centerScale;
        requestLayout();
    }
}
